package cn.mxz.robots.province;

import java.util.ArrayList;
import java.util.TreeMap;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 广西高考查询
 * 排名:完成
 * 去年排名:完成
 * 
 * @author chenmin <chenmin@mouge.wang>
 * @since 2015-06-22
 */
public class Guangxi extends Exam implements IRank
{

	public Guangxi(Student student)
	{
		super(student);
	}

	public static void main(String[] args)
	{
		Student student = new Student();
		student.setStudentId("010113519");
		student.setStudentPwd("961587");
		student.setType("w");
		Guangxi guangxi = new Guangxi(student);
		// guangxi.initRank();
		// guangxi.lastYearRank();
		System.out.println(guangxi.getTransformScore(7, 1, "l"));
		// System.out.println(guangxi.login(""));
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://222.216.29.73/Score/Score?cxmm=" + mStudent.getStudentPwd()
				+ "&ZKZH=" + mStudent.getStudentId());
		loginRequest.addHeader("Referer", "http://www.gxeea.cn/lstd/cg/gkcf/gkcf.htm");
		String loginHtml = curlGet(loginRequest);

		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("zkzh"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("准考证号码不正确"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("查询密码不正确"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String loginHtml)
	{
		JSONObject login = JSONObject.fromObject(regexMatch(loginHtml, "\\((.*)\\)"));

		JSONObject json = new JSONObject();
		json.put("exam_id", login.get("zkzh").toString().trim());
		json.put("name", login.get("ksxm").toString().trim());
		json.put("ranking", getRank(7 + 31, login.get("zf").toString(), mStudent.getType()));
		json.put("province", "广西省");

		JSONObject score = new JSONObject();
		score.put("语文", login.get("yw"));
		score.put("数学", login.get("sx"));
		score.put("外语", login.get("wy"));
		score.put("综合", login.get("zh"));
		score.put("总分", login.get("zf"));
		score.put("区外高校加分", login.get("zgf1"));
		score.put("区内高校加分", login.get("zgf2"));
		json.put("score", score.toString());

		return json.toString();
	}

	@Override
	public void initRank()
	{
		// 文史类http://www.gxeea.cn/2015score/wsl/index.htm
		// 理工类http://www.gxeea.cn/2015score/lgl/index.htm
		String rankHtml = curlGet(new Request("http://www.gxeea.cn/2015score/lgl/index.htm"));
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("a.scoreLink");
		int asSize = as.size();

		TreeMap<Integer, Integer> scoreRange = new TreeMap<Integer, Integer>();

		for (int i = 0; i < asSize; i++)
		{
			Element a = as.get(i);
			String raw = a.text();
			Integer score = Integer.valueOf(regexMatch(raw, "(\\d+)[\u5206]"));
			Integer counter = Integer.valueOf(regexMatch(raw, "(\\d+)[\u4eba]"));
			System.out.println(score);
			scoreRange.put(score, counter);
		}

		setRank(scoreRange, BEN_KE_LI_KE, 38);
	}

	@Override
	public void lastYearRank()
	{
		int index = 5;
		String rankUrl = String.format("http://gaokao.eol.cn/guang_xi_11327/20140622/t20140622_1137563%s.shtml",
				index == 0 ? "" : "_" + index);
		System.out.println(rankUrl);
		String rankHtml = curlGet(new Request(rankUrl));
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table[width=100%] > tbody > tr");
		if (index == 0)
			as.remove(0);// 删除表头
		System.out.println(as.size());

		int asSize = as.size();

		ArrayList<GaoKaoRank> ranks = new ArrayList<GaoKaoRank>();

		for (int j = 0; j < asSize; j++)
		{
			Elements tds = as.get(j).children();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setRanking(Integer.valueOf(tds.get(3).text()));
			rank.setScore(Integer.valueOf(tds.get(0).text()));
			rank.setCount(Integer.valueOf(tds.get(1).text()));
			rank.setType(BEN_KE_LI_KE);

			ranks.add(rank);
			System.out.println(rank);
		}

		setRank(ranks, 7);
	}
}
